package com.itkeien.streamprogramproject;

import com.itkeien.streamprogramproject.simpStream.Hero;


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.MonthDay;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;

public class Test {

    @org.junit.Test
    public void test1(){

        List<Integer> list1 = List.of(1,2,3);

        List list2 = List.of(4,5,6);
        List list3 = List.of(7,8,9);

        List<String> list00 = List.of("令狐冲", "段誉", "小龙女");


        List<Hero> heroes1 = List.of(
                new Hero("令狐冲", 100),
                new Hero("段誉", 100),
                new Hero("小龙女", 10),
                new Hero("段正淳", 101),
                new Hero("段正淳", 12),
                new Hero("段正淳", 1330),
                new Hero("段正淳", 10));




        //流的合并
//        Stream.concat(list1.stream(),list2.stream()).forEach(System.out::println);
//
//
////        流的截取
//
//        Stream.concat(list1.stream(),list2.stream()).skip(2).forEach(System.out::println);
//        Stream.concat(list1.stream(),list2.stream()).limit(2).forEach(System.out::println);
//
////        生成流
//
//        Stream.generate(Math::random).limit(5).forEach(System.out::println);//随机数
//
//        Stream.iterate(1,x->x+1).limit(5).forEach(System.out::println);//迭代
//
//        //流的查找与判断
//        boolean b = list1.stream().allMatch(x -> x >= 0);//判断是否全部符合条件
//        System.out.println(b);
//
//        boolean b1 = list1.stream().anyMatch(x -> x >= 0); //判断是否至少有一个符合条件
//        System.out.println(b1);
//
//        boolean b2 = list1.stream().noneMatch(x -> x >= 0);//判断是否没有符合条件
//        System.out.println(b2);
//
//        Integer integer = list1.stream().findAny().get();//找到任意一个元素
//        System.out.println(integer);
//
//        Integer integer1 = list1.stream().findFirst().get();//找到第一个元素
//
//        //流的去重与排序
//        Stream.concat(list1.stream(),list2.stream()).distinct().forEach(System.out::println);
//        Stream.concat(list1.stream(),list2.stream()).sorted().forEach(System.out::println);

//        //化简
//        Integer reduce = list1.stream().reduce(0, Integer::sum);//0是初始值 1是操作符
//        System.out.println(reduce);


//        Map<Integer, Map<String, Integer>> collect = list00.stream().collect(Collectors.groupingBy(x -> x.length(),
//                Collectors.toMap(x -> x, x -> x.length())));
//
//        Map<Integer, List<String>> collect1 = list00.stream().collect(Collectors.groupingBy(x -> x.length(), Collectors.toList()));
//        System.out.println(collect);
//        System.out.println(collect1);

//        Map<Integer, List<Hero>> collect = heroes1.stream().collect(groupingBy(Hero::getStrength));
//        System.out.println(collect);
//        Map<String, List<Hero>> collect1 = heroes1.stream().collect(groupingBy(Hero::getName));
//        System.out.println(collect1);
//
//        Map<Integer, List<String>> collect3 = heroes1.stream().collect(groupingBy(Hero::getStrength, mapping(x->x.getName(), toList()) ));
//        System.out.println(collect3);
//
//        Map<Integer, List<String>> filterCollect = heroes1.stream().collect(groupingBy(Hero::getStrength, filtering(x->x.getStrength()>10,
//                filtering(x->!x.getName().equals("段誉"), mapping(Hero::getName, toList())))));
//        System.out.println(filterCollect);
//
//        Map<Integer, List<String>> collect2 = heroes1.stream().
//                collect(groupingBy(Hero::getStrength, flatMapping(hero -> hero.getName().chars().mapToObj(Character::toString), toList())));

//        System.out.println(collect2);//{1330=[段, 正, 淳], 100=[令, 狐, 冲, 段, 誉], 101=[段, 正, 淳], 10=[小, 龙, 女, 段, 正, 淳], 12=[段, 正, 淳]}


    }

    @org.junit.Test
    public void test2(){
        try (Stream<String> lines = Files.lines(Path.of("src/main/resources/data.txt"))) {
            lines.limit(5).forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @org.junit.Test
    public void test3(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //新建当前时间字符串
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        System.out.println(now);
//        格式化时间
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒")));
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒 E")));

//        获取年月日
        System.out.println(YearMonth.from(LocalDateTime.now()));

        System.out.println(MonthDay.from(LocalDateTime.now()));
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        LocalDateTime parse = LocalDateTime.parse(now, dateTimeFormatter);
        System.out.println(parse);

        LocalDate parse1 = LocalDate.parse(now, dateTimeFormatter);
        System.out.println(parse1);






    }
}
